package tienda.zapateria.siszap.da;

import java.sql.Connection;
import java.util.ArrayList;
import org.rx.cr.dao.DAOAbstract;
import tienda.zapateria.siszap.be.BEDetalleCompra;

public class DADetalleCompra extends DAOAbstract<BEDetalleCompra> {

    public DADetalleCompra(Connection c) {
        setConnection(c);
    }

    @Override
    public int registrar(BEDetalleCompra be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_ins_detallecompra");
            setParameterInt(be.getCompraid());
            setParameterInt(be.getArticuloid());
            setParameterInt(be.getCantidad());

            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int actualizarRegistro(BEDetalleCompra be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_upd_detallecompra");
            setParameterInt(be.getDetallecompraid());
            setParameterInt(be.getCompraid());
            setParameterInt(be.getArticuloid());
            setParameterInt(be.getCantidad());

            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int eliminarRegistro(BEDetalleCompra be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_del_detallecompra");
            setParameterInt(be.getDetallecompraid());
            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public ArrayList<BEDetalleCompra> buscarReferencia(String string) throws Exception {
        try {
            clear();
            setStoreProcedure("zapateria.sp_sel_detallecompra");
            setParameterString(string);
            commitc();
            while (existResult()) {
                BEDetalleCompra be = new BEDetalleCompra();
                be.setDetallecompraid(getDataInt(1));
                be.setCompraid(getDataInt(2));
                be.setArticuloid(getDataInt(3));
                be.setCantidad(getDataInt(4));
                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public ArrayList<BEDetalleCompra> listar() throws Exception {
        try {
            clear();
            setStoreProcedure("zapateria.sp_sel_detallecompra");
            setParameterString("");
            commitc();
            while (existResult()) {
                BEDetalleCompra be = new BEDetalleCompra();
                be.setDetallecompraid(getDataInt(1));
                be.setCompraid(getDataInt(2));
                be.setArticuloid(getDataInt(3));
                be.setCantidad(getDataInt(4));

                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public BEDetalleCompra buscar(int i) throws Exception {
        try {
            BEDetalleCompra be = null;
            clear();
            setStoreProcedure("zapateria.sp_sel_detallecompra_id");
            setParameterInt(i);
            commitc();
            while (existResult()) {
                be = new BEDetalleCompra();
                be.setDetallecompraid(getDataInt(1));
                be.setCompraid(getDataInt(2));
                be.setArticuloid(getDataInt(3));
                be.setCantidad(getDataInt(4));

            }
            return be;
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

}
